﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;

namespace System.Data.SharePoint
{
    public class SharePointParameterCollection : DbParameterCollection
    {
        private bool _unboundFlag;
        private List<SharePointParameter> _parameterList = new List<SharePointParameter>();

        public override int Add(object value)
        {
            this._parameterList.Add(value as SharePointParameter);
            return this._parameterList.Count - 1;
        }

        public override void AddRange(Array values)
        {
            foreach (var value in values)
            {
                this.Add(value);
            }
        }

        public override void Clear()
        {
            this._parameterList.Clear();
        }

        public override bool Contains(string value)
        {
            return (IndexOf(value) != -1);
        }

        public override bool Contains(object value)
        {
            return this._parameterList.Contains(value as SharePointParameter);
        }

        public override void CopyTo(Array array, int index)
        {
            throw new NotImplementedException();
        }

        public override int Count
        {
            get { return this._parameterList.Count; }
        }

        public override System.Collections.IEnumerator GetEnumerator()
        {
            return _parameterList.GetEnumerator();
        }

        protected override DbParameter GetParameter(string parameterName)
        {
            return GetParameter(IndexOf(parameterName));
        }

        protected override DbParameter GetParameter(int index)
        {
            return _parameterList[index];
        }

        public override int IndexOf(string parameterName)
        {
            int x = _parameterList.Count;
            for (int n = 0; n < x; n++)
            {
                if (String.Compare(parameterName, _parameterList[n].ParameterName, StringComparison.OrdinalIgnoreCase) == 0)
                    return n;
            }
            return -1;
        }

        public override int IndexOf(object value)
        {
            return _parameterList.IndexOf(value as SharePointParameter);
        }

        public override void Insert(int index, object value)
        {
            this._unboundFlag = true;
            this._parameterList.Insert(index, value as SharePointParameter);
        }

        /// <summary>
        /// Returns true
        /// </summary>
        public override bool IsSynchronized
        {
            get { return true; }
        }

        /// <summary>
        /// Returns false
        /// </summary>
        public override bool IsFixedSize
        {
            get { return false; }
        }

        /// <summary>
        /// Returns false
        /// </summary>
        public override bool IsReadOnly
        {
            get { return false; }
        }

        public override void Remove(object value)
        {
            this._unboundFlag = true;
            this._parameterList.Remove(value as SharePointParameter);
        }

        public override void RemoveAt(string parameterName)
        {
            RemoveAt(IndexOf(parameterName));
        }

        public override void RemoveAt(int index)
        {
            this._unboundFlag = true;
            this._parameterList.RemoveAt(index);
        }

        protected override void SetParameter(string parameterName, DbParameter value)
        {
            SetParameter(IndexOf(parameterName), value);
        }

        protected override void SetParameter(int index, DbParameter value)
        {
            this._unboundFlag = true;
            this._parameterList[index] = value as SharePointParameter;
        }

        public override object SyncRoot
        {
            get { return null; }
        }
    }
}
